package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.GoodDTO;
import com.sky.dto.GoodPageQueryDTO;
import com.sky.entity.Good;
import com.sky.mapper.GoodMapper;
import com.sky.result.PageResult;
import com.sky.service.GoodService;
import com.sky.vo.GoodVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GoodServiceImpl implements GoodService {
    @Autowired
    private GoodMapper goodMapper;

    @Override
    public void save(GoodDTO goodDTO) {
        Good goods = new Good();
        BeanUtils.copyProperties(goodDTO, goods);
        goods.setStatus(1);
        goodMapper.insert(goods);
    }

    @Override
    public PageResult pageQuery(GoodPageQueryDTO goodPageQueryDTO) {
        PageHelper.startPage(goodPageQueryDTO.getPage(), goodPageQueryDTO.getPageSize());
        Page<GoodVO> page = goodMapper.pageQuery(goodPageQueryDTO);
        long total = page.getTotal();
        List<GoodVO> records = page.getResult();
        return new PageResult(total, records);

    }

    @Override
    public void deleteBatch(List<Long> ids) {
        //删除菜品表中的菜品数据
        for (Long id : ids) {
            goodMapper.deleteById(id);//后绪步骤实现
        }
    }

    @Override
    public GoodVO getByI(Long id) {
        //根据id查询菜品数据
        Good good = goodMapper.getById(id);
        //将查询到的数据封装到VO
        GoodVO goodVO = new GoodVO();
        BeanUtils.copyProperties(good, goodVO);
        return goodVO;
    }
}
